package com.vincent.kdp.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.vincent.baseapp.util.CommonUtil;
import com.vincent.kdp.R;
import com.vincent.kdp.bean.RePairRecordBean;
import com.vincent.kdp.http.RequestCallBack;
import com.vincent.kdp.repository.CheckGoodsRepository;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RepairGoodsActivity extends BaseActivity {
    @Bind(R.id.back_img)
    ImageView mBackImg;
    @Bind(R.id.repair_code)
    TextView mRepairCode;
    @Bind(R.id.repair_person)
    TextView mRepairPerson;
    @Bind(R.id.repair_detail)
    TextView mRepairDetail;
    @Bind(R.id.repair_buMen)
    TextView mRepairBuMen;
    @Bind(R.id.repair_yuanGong)
    TextView mRepairYuanGong;
    @Bind(R.id.repair_liuShui)
    TextView mRepairLiuShui;
    @Bind(R.id.repair_phone)
    TextView mRepairPhone;
    @Bind(R.id.repair_addTime)
    TextView mRepairAddTime;
    @Bind(R.id.repair_updateTime)
    TextView mRepairUpdateTime;
    @Bind(R.id.repair_look)
    RelativeLayout mRepairLook;

    private String barcode;
    private CheckGoodsRepository check;
    //维修进度ID
    private int id;

    @Override
    public int getLayOutId() {
        return R.layout.activity_repair_goods;
    }

    @Override
    public boolean isBufferKnife() {
        return true;
    }

    @Override
    public String getToolTitle() {
        return "维修";
    }

    @Override
    public void initView() {
        check=new CheckGoodsRepository();
        barcode=getIntent().getStringExtra("barcode");
        Log.i("code",barcode);
        getData(barcode);
    }

    private void getData(String barcode) {
        CommonUtil.showLoadingView(this);
        check.getRepairRecord(barcode, new RequestCallBack() {
            @Override
            public void onSuccess(Object object) {
                CommonUtil.dismissLoadingView(RepairGoodsActivity.this);
                RePairRecordBean bean= (RePairRecordBean) object;
                if(bean.getMsg().equals("请求成功")){
                    id=bean.getData().getId();
                    updataUi(bean.getData());
                }else{
                    Toast.makeText(RepairGoodsActivity.this,bean.getMsg(),Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFalse(String msg) {
                CommonUtil.dismissLoadingView(RepairGoodsActivity.this);
                Toast.makeText(RepairGoodsActivity.this,"未查询到该商品的维修记录",Toast.LENGTH_SHORT).show();
            }
        });
    }
    private void updataUi(RePairRecordBean.DataBean bean) {
        mRepairCode.setText(bean.getGoodBarCode());
        mRepairPerson.setText(bean.getMemberName());
        mRepairDetail.setText(bean.getDescription());
        mRepairBuMen.setText(bean.getCurrentDepartment());
        mRepairYuanGong.setText(bean.getCurrentEmployee());
        mRepairLiuShui.setText(bean.getSno());
        mRepairPhone.setText(bean.getPhone());
        mRepairAddTime.setText(getTime(bean.getAddtime()));
        mRepairUpdateTime.setText(getTime(bean.getUpdatetime()));
    }

    @OnClick({R.id.back_img})
    public void click() {
        finish();
        overridePendingTransition(com.vincent.baseapp.R.anim.fragment_left_in, com.vincent.baseapp.R.anim.fragment_right_out);
    }

    @OnClick({R.id.repair_look})
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.repair_look:
                //查看进度
                Intent intent=new Intent(this,RepairGoodsSpeedActivity.class);
                intent.putExtra("record",id);
                startActivity(intent);
                break;
        }
    }

    //把时间戳转换成时间
    public String getTime(long time){
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date=new Date(time);
        return format.format(date);
    }
}
